package com.qunar.sy.demo.service.impl;

import com.qunar.sy.demo.mapper.UserMapper;
import com.qunar.sy.demo.model.User;
import com.qunar.sy.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> selectAll() {
        List<User> userList = userMapper.selectAll();
        return userList;
    }

    @Override
    public User getById(Integer id) {
        return userMapper.selectByPrimaryKey(id);
    }

    @Override
    public void save(User user) {
        if (user.getId() == null) {
            insert(user);
        } else {
            update(user);
        }
    }

    @Override
    public void delete(Integer id) {
        userMapper.deleteByPrimaryKey(id);
    }

    private void update(User user) {
        userMapper.updateByPrimaryKey(user);
    }

    private void insert(User user) {
        userMapper.insert(user);
    }
}
